#!/bin/bash
mkdir -p build; pushd build;rm -rf debuginfo/ rpms/ test/ /usr/src/redhat/SOURCES/* /usr/src/redhat/SPECS/* /usr/src/redhat/BUILD/* ;popd;rpm -Uvh srpms/*;
basedir="`pwd`";
pushd /usr/src/redhat/SPECS;
rpm -e sodo libsobase --nodeps; rm -f /etc/so*.ini
rpm -e libscws --nodeps
rpm -e libsbase libevbase --nodeps;
rm -rf /usr/src/redhat/BUILD/* /usr/src/redhat/SRPMS/* /usr/src/redhat/RPMS/x86_64/* ${basedir}/debuginfo/* ${basedir}/test/*  ${basedir}/src/* ${basedir}/rpms/* ${basedir}/srpms/* /tmp/*;
buildrpm()
{
    name=$1;ver=$2;rel=$3;isdebug=$4;
    perl -i -p -e "s/^Version: .*/Version: ${ver}/" ${name}.spec \
    && perl -i -p -e "s/^Release: .*%/Release: ${rel}%/" ${name}.spec \
    && rpmbuild -ba ${name}.spec \
    && rpm -Uvh /usr/src/redhat/RPMS/x86_64/${name}-${ver}-${rel}.x86_64.rpm --force 
    [ "$?" != 0 ] && exit
    if [ "$isdebug" ]; 
    then
        cp ${name}.spec  ${name}_debug.spec \
        &&  perl -i -p -e "s/^%configure/%configure --enable-debug/" ${name}_debug.spec \
        &&  perl -i -p -e "s/^Release: .*%/Release: debug${rel}%/" ${name}_debug.spec \
        && rpmbuild -ba ${name}_debug.spec && rm -f ${name}_debug.spec
        [ "$?" != 0 ] && exit
    fi
}
#libevbase
buildrpm libevbase 0.0.18 9 debug
#libsbase
buildrpm libsbase 0.5.8 13 debug
#libscws
buildrpm libscws 1.1.1 2 
#libsobase
buildrpm libsobase 1.5.0 13
#sodo
buildrpm sodo 1.4.0 26
popd
perl -i -p -e "s@/tmp@/index/tmp@g" /etc/sodocd.ini;
service sodocd restart
rm -rf /tmp/*
#tarball
mkdir -p ${basedir}/{debuginfo,test,srpms,rpms,src}
cp -f /usr/src/redhat/SOURCES/*.tar.gz ${basedir}/src/ 
mv -f /usr/src/redhat/SRPMS/* ${basedir}/srpms/ 
mv -f /usr/src/redhat/RPMS/x86_64/*debuginfo* ${basedir}/debuginfo/
mv -f /usr/src/redhat/RPMS/x86_64/*debug* ${basedir}/test/
mv -f /usr/src/redhat/RPMS/x86_64/* ${basedir}/rpms/
pushd ${basedir}/;
mkdir -p backup sodo; datetime=`date +%Y%m%d%H%M%S`;tar -zcf backup/rpms-${datetime}.tar.gz rpms/libevbase* rpms/libsbase* rpms/libscws* rpms/libsobase* rpms/sodo*;tar -zcf backup/srpms-${datetime}.tar.gz srpms/libevbase* srpms/libsbase* srpms/libscws* srpms/libsobase* srpms/sodo* sodo.sh
popd;
